import request from "@/utils/request";
import { urlAddArgsByData } from "@/utils/index";
import { encryptedRsa } from "@/utils/crypto/encrypt-rsa";
import { encryptRSA, tokenName, baseURL } from "@/config/index";
import { useAuthStore } from '@/stores/auth';
import type { BaseEntity } from "@/types/baes/baes";
import { getPublicKey } from '@/utils/publicKey';


export interface UserInfoResponse extends BaseEntity {
  avatar: string;
  dataScope: string | null;
  email: string;
  enableSwitchTenant: string;
  encryptData: string | null;
  izSuperAdmin: boolean;
  mobile: string;
  no: string;
  passwordLevel: string;
  perms: string[];
  realName: string;
  remark: string;
  roles: any[];
  sign: string;
  switchTenantId: string | null;
  switchTenantUserId: string | null;
  tenantId: string;
  username: string;
}


export function sendSelfEmailCode(type: string) {
  return request({
    url: "/api/v1/operation/auth/email/create-code/" + type,
    method: "post",
  });
}

export function checkSelfEmailCode(type: string, code: string) {
  return request({
    url: "/api/v1/operation/auth/email/check-code/" + type + "/" + code,
    method: "post",
  });
}

export function sendEmailCode(data: any) {
  if (encryptRSA) {
    // 获得公钥
    const publicKey = getPublicKey() || '';
    // 加密数据
    let encrypted = encryptedRsa(data, publicKey);
    data = {
      encryptData: encrypted,
    };
  }
  return request({
    url: "/api/v1/common/email/create-code",
    method: "post",
    data,
  });
}

export function sendSelfMobileCode(type: string) {
  return request({
    url: "/api/v1/operation/auth/mobile/create-code/" + type,
    method: "post",
  });
}

export function checkSelfMobileCode(type: string, code: string) {
  return request({
    url: "/api/v1/operation/auth/mobile/check-code/" + type + "/" + code,
    method: "post",
  });
}

export function sendMobileCode(data: any) {
  if (encryptRSA) {
    // 获得公钥
    let publicKey = getPublicKey() || '';
    // 加密数据
    let encrypted = encryptedRsa(data, publicKey);
    data = {
      encryptData: encrypted,
    };
  }
  return request({
    url: "/api/v1/common/mobile/create-code",
    method: "post",
    data,
  });
}

export async function loginByCode(data: any) {
  if (encryptRSA) {
    // 获得公钥
    let publicKey = getPublicKey() || '';
    // 加密数据
    let encrypted = encryptedRsa(data, publicKey);
    data = {
      encryptData: encrypted,
    };
  }
  return request({
    url: "/system/login-by-code",
    method: "post",
    data,
  });
}

export async function login(data: any) {
  if (encryptRSA) {
    // 获得公钥
    let publicKey = getPublicKey() || '';
    // 加密数据
    let encrypted = encryptedRsa(data, publicKey);
    data = {
      encryptData: encrypted,
    };
  }
  return request({
    url: "/system/login",
    method: "post",
    data,
  });
}

export function getSlipCount(data: any) {
  return request({
    url: "/system/slipCount",
    method: "get",
    params: data,
  });
}

export function getUserInfo() {
  const authStore = useAuthStore();
  return request({
    url: "/api/v1/system/user/getInfo",
    method: "get",
    headers: {
      [tokenName]: authStore.accessToken,
    },
  });
}

export function getUserOrg() {
  const authStore = useAuthStore();
  return request({
    url: "/api/v1/system/user/getOrg",
    method: "get",
    headers: {
      [tokenName]: authStore.accessToken,
    },
  });
}

export function logout() {
  return request({
    url: "/system/logout",
    method: "post",
  });
}

export function register() {
  return request({
    url: "/register",
    method: "post",
  });
}

export function captcha(uuid: string) {
  // 请求地址
  let url = "/captcha";
  let data = {
    uuid: uuid,
    timestamp: new Date().getTime(),
  } as Record<string, string | number>;

  // 转换参数
  let params = Object.keys(data)
    .map(function (key) {
      return encodeURIComponent(key) + "=" + encodeURIComponent(data[key]);
    })
    .join("&");

  return baseURL + urlAddArgsByData(url, params);
}
